/**
 *Project: jh-framework
 *File: cn.xkshow.util.model.UrlParameter.java 
 *Author: admin@xkshow.cn with the copyrite
 ****************************************************************
 *   VERSION       DATE            BY       CHANGE/COMMENT
 * --------------------------------------------------------------
 *    1.0        2013-1-27        xkshow.cn         create
 ****************************************************************
 *note:
 ***************************************************************/ 
package cn.xkshow.util.model;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class UrlParameter
{
  public static String QS_START = "?";
  public static String QS_DELIM = "&";
  public static String QS_SEP = "=";

  public static String addParam(String path, String name, String value)
  {
    StringBuffer uri = new StringBuffer(path);
    boolean isQuery = path.indexOf(QS_START) >= 0;

    if (isQuery)
    {
      uri.append(QS_DELIM);
    }
    else
    {
      uri.append(QS_START);
    }
    uri.append(name);
    uri.append(QS_SEP);
    uri.append(value);
    return uri.toString();
  }

  public static String addParams(String path, Map parameters)
  {
    if (path == null)
    {
      path = new String();
    }

    if ((parameters == null) || (parameters.isEmpty()))
    {
      return path;
    }

    StringBuffer uri = new StringBuffer(path);

    Set entries = parameters.entrySet();
    for (Iterator i = entries.iterator(); i.hasNext(); )
    {
      Map.Entry e = (Map.Entry)i.next();

      boolean isQuery = uri.indexOf(QS_START) >= 0;
      if (isQuery)
      {
        uri.append(QS_DELIM);
      }
      else
      {
        uri.append(QS_START);
      }

      uri.append(e.getKey());
      uri.append(QS_SEP);
      uri.append(e.getValue());
    }

    return uri.toString();
  }
}
